﻿using System.Collections.Generic;
using System.Drawing;

namespace ProblemsSet
{
    public class Problem_91 : BaseProblem
    {
        public override object GetResult()
        {
            const int max = 50;

            long res = 0;

            var ptn = new Dictionary<Point, HashSet<Point>>();

            for (var x1 = 0; x1 <= max; x1++)
            {
                for (var x2 = 0; x2 <= max; x2++)
                {
                    for (var y1 = 0; y1 <= max; y1++)
                    {
                        for (var y2 = 0; y2 <= max; y2++)
                        {
                            var pt1 = new Point(x1, y1);
                            var pt2 = new Point(x2, y2);
                            if (ptn.ContainsKey(pt1) && ptn[pt1].Contains(pt2)) continue;
                            var a2 = (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
                            if (a2 == 0) continue;
                            var b2 = x1*x1 + y1*y1;
                            if (b2 == 0) continue;
                            var c2 = x2*x2 + y2*y2;
                            if (c2 == 0) continue;
                            if (c2 == a2 + b2 || a2 == c2 + b2 || b2 == a2 + c2)
                            {
                                if (!ptn.ContainsKey(pt1)) ptn.Add(pt1, new HashSet<Point>());
                                if (!ptn.ContainsKey(pt2)) ptn.Add(pt2, new HashSet<Point>());
                                ptn[pt1].Add(pt2);
                                ptn[pt2].Add(pt1);
                                res++;
                            }
                        }
                    }
                }
            }
            return res;
        }

        public override string Problem
        {
            get
            {
                return @"The points P (x1, y1) and Q (x2, y2) are plotted at integer co-ordinates and are joined to the origin, O(0,0), to form ΔOPQ.


There are exactly fourteen triangles containing a right angle that can be formed when each co-ordinate lies between 0 and 2 inclusive; that is,
0  x1, y1, x2, y2  2.


Given that 0  x1, y1, x2, y2  50, how many right triangles can be formed?";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 14234;
            }
        }

    }
}
